<?php
declare (strict_types = 1);

namespace app\api\middleware;

use app\Request;
use think\Response;

class Auth {

    public function handle(Request $request, \Closure $next) :Response {

        if (!$this->checkLogin($request)) {
            return show(config("status.not_login"), "没有登录", [], 401);
        }

        return $next($request);
    }

    /**
     *  判断访问的是否需要验证登录
     */
    protected function checkLogin(Request $request) : bool {

        $token = $request->header("access-token", "");
        if ($token == "") {
            return false;
        }

        if (!cache("?". config("redis.token_pre") . $token)) {
            return false;
        }

        return true;
    }
}